# Create your views here.
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.models import User
from cartoon_us.clan.models import clan
from cartoon_us.comment.models import comment

#creates a new clan and saves it in the database
def create(request):
    
    #ensures that the creator is a valid user
    if request.user.is_authenticated():
        if request.method=='POST':
            
            #checks for repeats
            try:
                temp=clan.objects.get(clanName=request.POST['clanName'])
                return render_to_response('clan/create_clan.html')
            
            #creates and saves the clan if there were no repeats
            except:
                c=clan(clanName=request.POST['clanName'],
                       clanIntro=request.POST['clanIntro'],
                       clanRule=request.POST['clanRule'],
                       clanFounder=request.user.username)
                c.save()
                request.user.my_clans_set.create(clan=c.clanName)
                path='C:/SDD/trunk/image/clanpics/'+c.clanName+'.jpg'
                destination=open(path,'wb+')
                
                #saves the clan image in the correct path
                for chunk in request.FILES['clanImage'].chunks():
                    destination.write(chunk)
                destination.close()
                
                #opens the newly created clan page
                return HttpResponseRedirect('../clan/'+c.clanName)
        #wrong post method, return to create clan page    
        else:
            return render_to_response('clan/create_clan.html')

    #User is not signed in, return to home page
    else:
        return HttpResponseRedirect('../home')

#sends specific clan data to to the html page
def showClan(request, clanName):

    #data will store the data to be used in the html file
    data={}
    data['clanName']=clanName

    #trys to find the clan in the database
    try:
        c=clan.objects.get(clanName=clanName)
    #if the clan does not exist, return to home page    
    except:
        return HttpResponseRedirect('../home')
    data['clanRule']=c.clanRule
    data['clanIntro']=c.clanIntro

    #each of the following for loops pulls data and adds it to the list 'data'
    members=[]
    for u in User.objects.all():
        try:
            c=u.my_clans_set.get(clan=clanName)
            members.append(u.username)
        except:
            c=1
    data['members']=members
    data['username']=request.user.username
    com=[]
    for c in comment.objects.all():
        if c.on=='clan' and c.to==clanName:
            com.append(c)
    data['comments']=com
    return render_to_response('clan/specific_clan.html', data)

#adds a user to a clan    
def join(request,clanName):
    if request.user.is_authenticated():
        try:
            t=request.user.my_clans_set.get(clan=clanName)
        except:
            request.user.my_clans_set.create(clan=clanName)
    return HttpResponseRedirect('../'+clanName)

#redirects to the default clan page   
def default(request):
    return HttpResponseRedirect('../../list/clan/1')
